// const AdminModel = require('../models/AdminModel.js');
const ArticleModel = require('../models/ArticleModel.js');
const ArticleReviewModel = require('../models/ArticleReviewModel.js');
const UserModel = require('../models/UserModel.js');

const ArticleStatuModel = require('../models/ArticleStatuModel.js');
const jwt = require('jsonwebtoken');
const moment = require('moment');



//查询所有文章
exports.getAllArticle = async (ctx) => {
	try {
		const users = await ArticleModel.findAll({
			attributes: ['id', 'name', 'detail', 'img1', 'img2', 'img3']
		});
		ctx.body = {
			code: 0,
			data: users
		}
	}
	catch (e) {
		ctx.body = {
			code: 10000,
			message: '网络出错'
		}
	}
}

//获取文章详情
exports.getArticleDetail = async (ctx) => {


	const id = ctx.query.id;
	try {
		const articles = await ArticleModel.findOne({
			attributes: ['id', 'name', 'detail', 'img1', 'img2', 'img3'],
			where: {
				id: id
			},
		});




		ctx.body = {
			code: 0,
			data: {

				name: articles.name,
				detail: articles.detail,
				img1: articles.img1,
				img2: articles.img2,
				img3: articles.img3,

			}
		}
	}
	catch (e) {
		ctx.body = {
			code: 10000,
			message: '网络错误'
		}
	}
}




//获得文章详情页评论区数据
exports.getreviewMsg = async (ctx) => {
	const id = ctx.query.id;
	try {
		const msgs = await ArticleReviewModel.findAll({
			where: {
				articleid: id
			},
			order: [
				['createtime', 'DESC']
			],
		});
		if (msgs.length === 0) {
			ctx.body = {
				code: 0,
				data: []
			};
			return;
		}

		let msgList = [];
		for (let msg of msgs) {
			let nickname = '';
			let img = '';
			let user = await UserModel.findOne({
				attributes: ['nickname', 'headimg'],
				where: {
					id: msg.dataValues.userid
				}
			});

			if (!user) {
				nickname = '该用户已注销'
			} else {
				nickname = user.nickname
				img = user.headimg
			}



			msgList.push({
				id: msg.dataValues.id,
				content: msg.dataValues.articledetail,
				img: img,
				asker: nickname,
				time: moment(msg.dataValues.createtime).add('hours', 8).format('MM-DD HH:mm'),
				// reply:replyObj
			})
		}

		ctx.body = {
			code: 0,
			data: msgList
		}
	}
	catch (e) {
		ctx.body = {
			code: 10000,
			message: '网络错误1'
		}
	}
}

//评论文章
exports.reviewarticleMsg = async (ctx) => {
	const token = ctx.request.body.token;
	try {
		const res = ArticleReviewModel.create({
			userid: jwt.verify(token, 'chambers'),
			articleid: ctx.request.body.articleid,
			articledetail: ctx.request.body.msg,
			createtime: new Date()
		})
		ctx.body = {
			code: 0,

		}
	}
	catch (e) {
		ctx.body = {
			code: 10000,
			message: '网络出错'
		}
	}
}





//点赞文章
exports.thumbarticleMsg = async (ctx) => {
	const token = ctx.request.body.token;
	const userids = jwt.verify(token, 'chambers')
	// const status =[]
	var status = []

	try {
			status = await ArticleStatuModel.findAll({
				attributes: ['id', 'userid', 'articleid', 'thumbs'],
				where: {
					userid: userids,
					articleid:ctx.request.body.articleid,
				},
			})
if (ctx.request.body.thumbs>=10) {
	ctx.request.body.thumbs==10
}

		if (status[0].articleid == ctx.request.body.articleid && status[0].userid == userids) {
			var thisthumbs = (status[0].thumbs >= ctx.request.body.thumbs? status[0].thumbs : ctx.request.body.thumbs);

			const res = await ArticleStatuModel.update({
				// userid:userids,
				// articleid:ctx.request.body.articleid,
				thumbs: thisthumbs,

				liked:'1',
				createtime: new Date()

			},
				{
					where: {
						userid: userids,
						articleid:ctx.request.body.articleid,
					},
				}
			)
		}

		//  else  {
		// 	const res1 = await ArticleStatuModel.create({
		// 		userid: userids,
		// 		articleid: ctx.request.body.articleid,
		// 		thumbs: ctx.request.body.thumbs,

		// 		liked: "1",
		// 		createtime: new Date()
		// 	})

		// }






		ctx.body = {
			code: 0,
			data:status[0].thumbs,
	// msg:data

		}
	}
	catch (e) {
		ctx.body = {
			code: 10000,
			message: '点赞出现错误',
			msg: status.length == 0,

		}
	}
}

//文章状态
exports.getstatusMsg = async (ctx) => {
	// let id = ;
	try {
		const msgs = await ArticleStatuModel.findAll({
			where: {
				articleid: ctx.query.id
			}
		});
		if (msgs.length === 0) {
			ctx.body = {
				code: 0,
				// data:msg
			};
			return;
		}
var thumbs_number=0;
	
		for (let msg of msgs) {
		
			thumbs_number +=msg.thumbs


		}

		ctx.body = {
			code: 0,
			data: thumbs_number
		}
	}
	catch (e) {
		ctx.body = {
			code: 10000,
			message: '网络错误1'
		}
	}
}

















// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1001', '101', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1001', '102', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1001', '103', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1001', '104', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1001', '105', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1001', '106', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1001', '107', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1001', '108', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1001', '109', '0',0, '2022-05-08 21:24:03');

// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1002', '101', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1002', '102', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1002', '103', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1002', '104', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1002', '105', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1002', '106', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1002', '107', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1002', '108', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1002', '109', '0',0, '2022-05-08 21:24:03');

// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1003', '101', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1003', '102', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1003', '103', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1003', '104', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1003', '105', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1003', '106', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1003', '107', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1003', '108', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1003', '109', '0',0, '2022-05-08 21:24:03');

// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1004', '101', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1004', '102', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1004', '103', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1004', '104', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1004', '105', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1004', '106', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1004', '107', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1004', '108', '0',0, '2022-05-08 21:24:03');
// INSERT INTO `mall`.`articlestatus` ( `userid`, `articleid`, `liked`,`thumbs`, `createtime`) VALUES ( '1004', '109', '0',0, '2022-05-08 21:24:03');























